#include "framework.h"
#include "ui_framework.h"

FrameWork::FrameWork(QWidget *parent)
    : BaseFrameWork(parent)
    , ui(new Ui::FrameWork)
{
    ui->setupUi(this);
    this->setWindowFlag(Qt::FramelessWindowHint);

    m_pUserManager = UserManager::instance();
    m_pTitleBar = new Titlebar(this);
    m_pLoginWidget = new LoginWidget(this);

    m_pStatusBar = new Statusbar(this);
    m_pUart = new Uart(this);
    m_pRegisterWidget = new RegisterWidget(this);
    m_pRegisterWidget->setUserManager(m_pUserManager);
    m_pGetBackPasswordWidget = new GetBackPasswordWidget(this);
    m_pMainSystemWidget = new MainSystemWidget(this);

    m_pStackedLayout = new QStackedLayout(ui->mainFrameLayout);
    m_pStackedLayout->addWidget(m_pLoginWidget);
    m_pStackedLayout->addWidget(m_pRegisterWidget);
    m_pStackedLayout->addWidget(m_pGetBackPasswordWidget);
    m_pStackedLayout->addWidget(m_pMainSystemWidget);
    m_pStackedLayout->addWidget(m_pUart);


    ui->titleBarLayout->addWidget(m_pTitleBar);
    ui->statusBarLayout->addWidget(m_pStatusBar);

    connect(m_pLoginWidget,&LoginWidget::display,m_pStackedLayout,&QStackedLayout::setCurrentIndex);
    //connect(m_pLoginWidget, &LoginWidget::userDisplay, m_pMainSystemWidget, &MainSystemWidget::switchToRole);
    connect(m_pLoginWidget, &LoginWidget::userDisplay, [=](int role) {
        m_pMainSystemWidget->switchToRole(role);   // 切换到对应界面
    });

    connect(m_pRegisterWidget,&RegisterWidget::display,m_pStackedLayout,&QStackedLayout::setCurrentIndex);
    connect(m_pLoginWidget, &LoginWidget::sendMessage, m_pStatusBar, &Statusbar::onRecvMessage);
    connect(m_pRegisterWidget, &RegisterWidget::sendMessage, m_pStatusBar, &Statusbar::onRecvMessage);
    connect(m_pGetBackPasswordWidget, &GetBackPasswordWidget::sendMessage, m_pStatusBar, &Statusbar::onRecvMessage);
    connect(m_pGetBackPasswordWidget,&GetBackPasswordWidget::display,m_pStackedLayout,&QStackedLayout::setCurrentIndex);

}


FrameWork::~FrameWork() {
    // 先销毁子窗口，再销毁布局
    delete m_pMainSystemWidget;
    delete m_pLoginWidget;
    delete m_pRegisterWidget;
    delete m_pGetBackPasswordWidget;
    delete m_pUart;
    delete m_pStackedLayout;
    delete m_pTitleBar;
    delete m_pStatusBar;
    delete ui;
}
